package com.mayisoftware.mayioa.business.worktime.impl.serviceImpl;


import com.mayisoftware.mayioa.business.worktime.api.domain.*;
import com.mayisoftware.mayioa.business.worktime.api.service.IWorkTimeDataService;
import com.mayisoftware.mayioa.business.worktime.impl.dao.TmWorktimeDataMapper;
import com.mayisoftware.mayioa.common.impl.utils.DateUtils;
import com.mayisoftware.mayioa.platform.api.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class WorktimeDataImpl implements IWorkTimeDataService {
    @Autowired
    private ISysDictDataService dictService;
    @Autowired
    private TmWorktimeDataMapper dataMapper;

    @Override
    public List<TMWrokTimeFlow> queryDoubtRepairWorktime(WrokTimeImportInfo record) {
        List<TMWrokTimeFlow>  doubtRepairs = dataMapper.queryDoubtRepairWorktime(record);
        return doubtRepairs;
    }
    @Override
    public List<TMWrokTimeFlow> queryMonthOweWorktime(WrokTimeImportInfo record) {
        List<TMWrokTimeFlow>  doubtRepairs = dataMapper.queryMonthOweWorktime(record);
        return doubtRepairs;
    }




    private int getHour(String hour){
        int converInt = -1;
        if (hour==null||hour.length()<=2){
            return converInt;
        }
        try {
            hour = hour.substring(0,2);
            converInt = Integer.parseInt(hour);
        }catch (Exception ex){
            return converInt;
        }
        return converInt;
    }
    @Override
    public List<TMWorktimeDetailFlow> queryExtraGreater8Hours(WrokTimeImportInfo record) {
        return dataMapper.getExtraGreater8Hours(record);
    }

    @Override
    public List<TMWorktimeMonthInfo> queryLackHasExtra(WrokTimeImportInfo record) {
        List<TMWorktimeMonthInfo> retList = new ArrayList<>();

        List<TMWorktimeMonthInfo> monthInfos =  dataMapper.queryLackHasExtra(record);

        retList = groupMonthInfo(monthInfos);
        return retList;
    }

    private List<TMWorktimeMonthInfo> groupMonthInfo(List<TMWorktimeMonthInfo> monthInfos){
        List<TMWorktimeMonthInfo> retList = new ArrayList<>();
        if(monthInfos==null||monthInfos.size()==0){
            return retList;
        }
        Map<String,List<TMWorktimeMonthInfo>> kes = new HashMap<>();
        for(int i =0 ; i< monthInfos.size();++i){
            TMWorktimeMonthInfo monthInfo = monthInfos.get(i);
            String key = monthInfo.getMonth()+"_"+monthInfo.getId()+"_"+monthInfo.getName();
            List<TMWorktimeMonthInfo> groups = null;
            if (kes.containsKey(key)){
                groups = kes.get(key);
            }else{
                groups = new ArrayList<>();
            }

            groups.add(monthInfo);
            kes.put(key,groups);
        }

        for (String key :kes.keySet()){
            TMWorktimeMonthInfo addMonthInfo = addToOneMonthInfo(kes.get(key));
            if (addMonthInfo!=null){
                retList.add(addMonthInfo);
            }
        }
        return retList;
    }

    private TMWorktimeMonthInfo addToOneMonthInfo(List<TMWorktimeMonthInfo> monthInfos){
        TMWorktimeMonthInfo retInfo = null;

        if (monthInfos==null ||monthInfos.size()==0){
            return  retInfo;
        }

        for (int i =0 ; i < monthInfos.size();++i){
            TMWorktimeMonthInfo tgtInfo = monthInfos.get(i);

            if (i==0){
                retInfo = tgtInfo;
                continue;
            }
            double oweHours = retInfo.getMinServiceHours()+tgtInfo.getMinServiceHours()
                    -(retInfo.getActualServiceHours()+tgtInfo.getActualServiceHours());
            BigDecimal   bigDecimal   =   new BigDecimal(oweHours);
            double   f1   =   bigDecimal.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
            retInfo.setOweServiceHours(f1);
            retInfo.setExtraHours(retInfo.getExtraHours()+tgtInfo.getExtraHours());
            retInfo.setMinServiceHours(retInfo.getMinServiceHours()+tgtInfo.getMinServiceHours());
            retInfo.setActualServiceHours(retInfo.getActualServiceHours()+tgtInfo.getActualServiceHours());
            retInfo.setPo(retInfo.getPoNum()+","+tgtInfo.getPoNum());
        }

        return retInfo;
    }
    @Override
    public List<TMWorktimeMonthInfo> queryLackWorktime(WrokTimeImportInfo record) {
        List<TMWorktimeMonthInfo> retList = new ArrayList<>();
        List<TMWorktimeMonthInfo> monthInfos =  dataMapper.queryLackWorktime(record);
        retList = groupMonthInfo(monthInfos);
        return retList;
    }

    @Override
    public List<TMWorktimeDetailFlow> queryExtraLess8Hours(WrokTimeImportInfo record) {
        return dataMapper.queryExtraLess8Hours(record);
    }

    @Override
    public List<TMWorktimeDetailFlow> queryHasDoubleFlows(WrokTimeImportInfo record) {
        return dataMapper.queryHasDoubleFlows(record);
    }

    @Override
    public List<TMWorktimeLeaveFlow> queryGreater3TimesLeave(WrokTimeImportInfo record) {
        return dataMapper.queryGreater3TimesLeave(record);
    }

    public void convertToLabel(WrokTimeImportInfo record){
        record.setPdu(getDictLabel("human_pdu",record.getPdu()));
        record.setRegion(getDictLabel("check_region",record.getRegion()));
        record.setMonth(getDictLabel("data_month",record.getMonth()));
    }

    @Override
    public DoubtRepairInfo getRepairInfo(WrokTimeImportInfo repairInfo) {
        return null;
    }

    @Override
    public int updateRepairInfo(DoubtRepairInfo repairInfo) {
        return 0;
    }


    private String getDictLabel(String dictType, String dictValue) {
        return  dictService.selectDictLabel(dictType,dictValue);
    }


}
